import z from 'zod'
import { request } from '.'

// 根据学校获取班级
const getClassListRequestSchema = z.object({
  school: z.string(),
})
export function getClassList(data: z.infer<typeof getClassListRequestSchema>) {
  return request({
    method: 'POST',
    url: '/get-classes-by-school',
    data,
    dataValidator: getClassListRequestSchema,
    responseValidator: z.object({
      entries: z.array(z.object({
        classes: z.array(z.object({
          active: z.boolean(),
          avatar: z.null(),
          cid: z.string(),
          gradeInt: z.number(),
          invitationCode: z.string(),
          lastModifiedTime: z.string(),
          name: z.string(),
          school: z.string(),
          stage: z.union([z.null(), z.string()]),
          stageType: z.number(),
          startYear: z.number(),
          studentsNum: z.number(),
          teachers: z.array(z.object({
            birthday: z.string(),
            gender: z.string(),
            name: z.string(),
            data: z.string(),
            school: z.string(),
            stageType: z.number(),
            tid: z.string(),
          })),
        })),
        gradeInt: z.number(),
      })),
    }),
  })
}

// 创建/修改班级
const createOrUpdateClassRequestSchema = z.object({
  gradeInt: z.number(),
  name: z.string(),
  school: z.string(),
  stageType: z.number(),
  cid: z.string().optional(),
})
export function createOrUpdateClass(data: z.infer<typeof createOrUpdateClassRequestSchema>) {
  return request({
    method: 'POST',
    url: '/set-class',
    data,
    dataValidator: createOrUpdateClassRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        active: z.boolean(),
        avatar: z.null(),
        classId: z.string(),
        gradeInt: z.number(),
        id: z.number(),
        invitationCode: z.string(),
        name: z.string(),
        readingCount: z.null(),
        school: z.string(),
        stage: z.null(),
        stageType: z.number(),
        startYear: z.number(),
        studentCount: z.number(),
        teacherCount: z.number(),
        timestamp: z.string(),
      }),
    }),
  })
}
// 根据班级ID获取班级信息
const getClassByIdRequestSchema = z.object({
  cid: z.string(),
})
export function getClassById(data: z.infer<typeof getClassByIdRequestSchema>) {
  return request({
    method: 'POST',
    url: '/get-class-by-id',
    data,
    dataValidator: getClassByIdRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        active: z.boolean(),
        avatar: z.null(),
        cid: z.string(),
        gradeInt: z.number(),
        invitationCode: z.string(),
        lastModifiedTime: z.string(),
        name: z.string(),
        school: z.string(),
        stage: z.union([z.null(), z.string()]),
        stageType: z.number(),
        startYear: z.number(),
        studentsNum: z.number(),
        teachers: z.array(z.object({
          birthday: z.union([z.null(), z.string()]).optional(),
          gender: z.union([z.null(), z.string()]).optional(),
          name: z.union([z.null(), z.string()]).optional(),
          data: z.union([z.null(), z.string()]).optional(),
          school: z.union([z.null(), z.string()]).optional(),
          stageType: z.union([z.number(), z.null()]).optional(),
          tid: z.union([z.null(), z.string()]).optional(),
        }),
        ),
      }),
    }),
  })
}

// 删除班级
const deleteClassRequestSchema = z.object({
  cid: z.string(),
})
export function deleteClass(data: z.infer<typeof deleteClassRequestSchema>) {
  return request({
    method: 'POST',
    url: '/delete-class-by-id',
    data,
    dataValidator: deleteClassRequestSchema,
  })
}

// 获取班级学生列表
const getClassStudentsRequestSchema = z.object({
  cid: z.string(),
})
export function getClassStudents(data: z.infer<typeof getClassStudentsRequestSchema>) {
  return request({
    method: 'POST',
    url: '/get-students-by-class',
    data,
    dataValidator: getClassStudentsRequestSchema,
    responseValidator: z.object({
      entries: z.array(z.object({
        birthday: z.union([z.null(), z.string()]).optional(),
        creationTime: z.union([z.null(), z.string()]).optional(),
        gender: z.union([z.null(), z.string()]).optional(),
        historyClasses: z.union([z.array(z.object({
          cid: z.union([z.null(), z.string()]).optional(),
          joinedTime: z.union([z.null(), z.string()]).optional(),
          quitedTime: z.union([z.null(), z.string()]).optional(),
          stageType: z.union([z.number(), z.null()]).optional(),
        })), z.null()]).optional(),
        name: z.union([z.null(), z.string()]).optional(),
        phone: z.union([z.null(), z.string()]).optional(),
        school: z.union([z.null(), z.string()]).optional(),
        sid: z.union([z.null(), z.string()]).optional(),
      })),
    }),
  })
}

// 班级新增教师
const addTeacherToClassRequestSchema = z.object({
  cid: z.string(),
  teacher: z.object({
    name: z.string(),
    phone: z.string(),
  }),
})
export function addTeacherToClass(data: z.infer<typeof addTeacherToClassRequestSchema>) {
  return request({
    method: 'POST',
    url: '/add-teacher-to-class',
    data,
    dataValidator: addTeacherToClassRequestSchema,
  })
}

// 移除班级中的教师
const removeTeacherFromClassRequestSchema = z.object({
  cid: z.string(),
  teacher: z.object({
    name: z.string(),
    phone: z.string(),
  }),
})
export function deleteTeacherFromClass(data: z.infer<typeof removeTeacherFromClassRequestSchema>) {
  return request({
    method: 'POST',
    url: '/remove-teacher-from-class',
    data,
    dataValidator: removeTeacherFromClassRequestSchema,
  })
}

// 从班级中移除学生
const removeStudentFromClassRequestSchema = z.object({
  cid: z.string(),
  student: z.object({
    sid: z.string(),
  }),
})
export function removeStudentFromClass(data: z.infer<typeof removeStudentFromClassRequestSchema>) {
  return request({
    method: 'POST',
    url: '/remove-student-from-class',
    data,
    dataValidator: removeStudentFromClassRequestSchema,
  })
}

// 隐藏、显示班级
const toggleClassVisibilityRequestSchema = z.object({
  cid: z.string(),
})
export function toggleClassVisibility(data: z.infer<typeof toggleClassVisibilityRequestSchema>) {
  return request({
    method: 'POST',
    url: '/set-inactive-class',
    data,
    dataValidator: toggleClassVisibilityRequestSchema,
  })
}
